home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / RTF / search_path.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  135 lines

  1. //  search_path.C - 
  2.  
  3. // Copyright (c) 1990, 1991, 1992 Convex Computer Corporation
  4. // All rights reserved.
  5.  
  6. /**********************************************************************
  7. ** File:    search_path.C
  8. ** Ident:    $CHeader: search_path.C 1.1 92/06/11 18:44:50 $
  9. ** Language:    C++
  10. ** Purpose:
  11.     
  12. ** Types
  13.     None
  14. ** Constants:
  15.  
  16. ** Global Data:
  17.  
  18. ** Static Data:
  19.  
  20. ** Global Functions: 
  21.  
  22. ** Public methods:
  23.  
  24. ** Private methods:
  25.  
  26. ** Static Public methods:
  27.  
  28. ** Static Private methods:
  29.  
  30. ** Notes:
  31.  
  32. ** Revision History:
  33. ** Who    Date     Description
  34.    ---  -------- ----------------------------------------------------
  35.    DWC  1-31-92   initial implementation   
  36. **********************************************************************/
  37.  
  38. #include "search_path.h"
  39.  
  40. #include <assert.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44.  
  45. #include "debug.h"
  46.  
  47. char SearchPath::buffer[max_path_len+1];
  48. int SearchPath::len;
  49.  
  50. void
  51. SearchPath::grow(int tot)
  52. {
  53.   assert(tot>0);
  54.  
  55.   if(tot >= allocated){
  56.     allocated = allocated * 3/2 + 10;
  57.     Filename* n = new char*[allocated];
  58.     bzero(n, allocated*sizeof(Filename)); //@@ is this redundant?
  59.     if(dirs){
  60.       bcopy(dirs, n, dir_qty * sizeof(Filename));
  61.       current = n + (current - dirs);
  62.       delete [] dirs;
  63.     }
  64.     dirs = n;
  65.   }
  66. }
  67.  
  68. int
  69. SearchPath::add_directories(Path path){
  70.   if(!path || !*path)
  71.     return dir_qty;
  72.   
  73.   register const char *s, *e;
  74.   register char *d;
  75.   int len;
  76.   for(s = path; s && *s; s = e ? e+1 : 0){
  77.     e = strchr(s, ':');
  78.     len = e ? e-s : strlen(s);
  79.  
  80.     grow(dir_qty+1);
  81.     d = new char[len+1];
  82.     strncpy(d, s, len);
  83.     d[len] = 0;
  84.     dirs[dir_qty++] = (Filename)d;
  85.   }
  86. }
  87.  
  88. SearchPath::Filename SearchPath::find(Filename f, int mode, const char* ext)
  89. {
  90.   if(strlen(f) > max_name_len)
  91.     return 0;
  92.  
  93.   Filename d, e, ee;
  94.   int l;
  95.  
  96.   for(d=first_dir(); d; d=next_dir()){
  97.     for(e = ext ? ext : ""; e; (e=ee) && e++){
  98.       (ee = strchr(e, ':')) ? (l=ee-e) : (l=strlen(e));
  99.       if( (len = strlen(d) + strlen(f) + l) > max_path_len)
  100.     continue;
  101.     
  102.       sprintf(buffer, "%s/%s%.*s", d, f, l, e);
  103.  
  104.       if(0 == access(buffer, mode)){
  105.     debug(("found %s at %s\n", f, buffer));
  106.     return buffer;
  107.       }
  108.     }
  109.   }
  110.   return 0;
  111. }
  112.  
  113. char*
  114. SearchPath::lookup(Filename f, int mode, const char* ext)
  115. {
  116.   if(find(f,mode, ext)){
  117.     char * ret = new char[len+1];
  118.     strcpy(ret, buffer);
  119.     return ret;
  120.   }else
  121.     return 0;
  122. }
  123.  
  124. FILE*
  125. SearchPath::open(Filename f, const char* ext)
  126. {
  127.   if(find(f, R_OK, ext))
  128.     return fopen(buffer, "r");
  129.   else
  130.     return 0;
  131. }
  132.     
  133.  
  134.   
  135.